mui.init();


mui('.mui-content .mui-switch').each(function() { //循环所有toggle

	//toggle.classList.contains('mui-active') 可识别该toggle的开关状态
//	this.parentNode.querySelector('span').innerText = '状态：' + (this.classList.contains('mui-active') ? 'true' : 'false');
	/**
	 * toggle 事件监听
	 */
	this.addEventListener('toggle', function(event) {
		var rank =event.detail.isActive?1:0;
		var url = UrlConfig.updateIsRank;
		var pars ={rank:rank};
		MuiEventMgr.ajax(url,{pars:pars,
	  	sfn : function(data){
//	  		var msg = data.status;
//	  		if(msg=='true'){
//	  			mui.alert("修改成功");
//	  			mui.back();
//	  		}else{
//	  			mui.alert("修改失败")
//	  		}
	  	},ffn : function(err){
	  		mui.alert("获取数据失败");
	  	}
	 });
		//event.detail.isActive 可直接获取当前状态
//		this.parentNode.querySelector('span').innerText = '状态：' + (event.detail.isActive ? 'true' : 'false');
		
	});
	});

/**
 * 回调用当前“刷新”事件 
 */
window.addEventListener('reload',function(event){
	loadDatas();
});


mui.plusReady(function(){

	loadDatas();

	(function(){
		var targetPageId = plus.webview.currentWebview().id;
		plus.storage.setItem(KeyCfg.targetPageId,targetPageId);
		/**-------- 修改登录密码  --------*/
		var reset_login_pwd = CmwMcore.$('reset_login_pwd');
		MeventMgr.onTouchstart(reset_login_pwd,function(event,self){			
			MuiEventMgr.open('reset_login_pwd.html',{});	
		});	
		
		/**-------- 修改交易密码  --------*/
		var reset_detail_pwd = CmwMcore.$('reset_detail_pwd');
		MeventMgr.onTouchstart(reset_detail_pwd,function(event,self){
			MuiEventMgr.open('reset_detail_pwd.html',{});	
		});	
		
		/**-------- 修改邮箱  --------*/
		var reset_email = CmwMcore.$('reset_email');
		MeventMgr.onTouchstart(reset_email,function(event,self){
			var email1 = $("#email1").val();
			if(email1=='未设置')
			MuiEventMgr.open('semail.html',{});	
			else
			MuiEventMgr.open('reset_email.html',{});	
		});	
		
		/**-------- 修改安全问题  --------*/
		var reset_safe = CmwMcore.$('reset_safe');
		MeventMgr.onTouchstart(reset_safe,function(event,self){
			var safe1 = $("#safe1").val();
			if(safe1=='未设置')
			MuiEventMgr.open('sproblem.html',{});	
			else
			MuiEventMgr.open('reset_safety_problem.html',{});	
		});	
		
		/**-------- 修改手机号码   --------*/
		var reset_phone = CmwMcore.$('reset_phone');
		MeventMgr.onTouchstart(reset_phone,function(event,self){
			
			MuiEventMgr.open('reset_phone.html',{});	
		});	
		
		/**-------- 修改昵称   --------*/
		var reset_name = CmwMcore.$('reset_name');
		MeventMgr.onTouchstart(reset_name,function(event,self){
			MuiEventMgr.open('reset_name.html',{});	
		});
		
		var level = CmwMcore.$('level');
		MeventMgr.onTouchstart(level,function(event,self){
			MuiEventMgr.open('../srealName/srealName.html',{});	
		});
	})();
	
	
});




	
function loadDatas(){
	var url = UrlConfig.getUserInfo;
	var guid = plus.storage.getItem(KeyCfg.userId);
	var pars={guid:guid};
	// 弹出系统等待对话框   
	//var w = mui.plus.nativeUI.showWaiting( "正在加载..." );
	MuiEventMgr.ajax(url,{pars:pars,
	  	sfn : function(data){
	  		callback(data);
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  		//mui.plus.nativeUI.closeWaiting();
	  	}
	 });
}

function callback(data){
	$(".mui-loading").hide();
	var user =data;
	$("#username").val(user.userName);
	var safe = data.safe;
	var realName = user.realName;
	var card = user.card;
	var status = user.isRank;
	var transPassword = user.transPassword;
	if(transPassword)
	$("#tranPwd").html("交易密码：******");
	else
	$("#tranPwd").html("交易密码：未设置");
	if(status){
		var mySwitch = document.getElementById("mySwitch");
		mySwitch.classList.add('mui-active');
	}else{
		var mySwitch = document.getElementById("mySwitch");
		mySwitch.classList.remove('mui-active');
	}
	if(!realName){
		realName="未设置";
		$("#prompt").show();
	}else{realName=realName.substring(0,1)+"**";}
	if(safe==true){safe="已设置";}
	else{safe="未设置";}
	if(!card){card="未设置";}
	else{card=card.substring(0,4)+"***********"+card.substring(card.length-4,card.length);}	
	var nickName = user.nickName;
	if(!nickName){nickName="未设置";}
//	var isRank = user.isRank
//	if(isRank==true){isRank="参加";}
//	else{isRank="不参加";}
	var phone = user.phone;
	if(!phone){phone="未设置";}
	else{phone = phone.substring(0,3)+"****"+phone.substring(phone.length-4,phone.length);}
	var email = user.erroremial;
	if(!email){email="未设置";}
	var guid= user.guid;
	var url = UrlConfig.muser_register+'/security/register.htm?activeId='+guid;
	
	$("#realName").html("真实姓名："+realName);
	$("#card").html("身份证号："+card);
	$("#email").html("电子邮箱："+email);
	$("#safe").html("安全问题："+safe);
	$("#phone").html("手机号码："+phone);
	$("#nickName").html("昵称："+nickName);
	$("#status").val(status);
	$("#promote").html("推广链接："+url);
	
	$("#email1").val(email);
	$("#safe1").val(safe);
	$("#phone1").val(phone);
	$("#nickName1").val(nickName);
	$("#tranPwd1").val(transPassword);

	
	
	
	
//	var html=getHtml(data);
//	$("#item1_loanRecords").html(html);
	addClick();
	
}

function getHtml(data){	
	var user =data;
	$("#username").val(user.userName);
	var safe = data.safe;
	var realName = user.realName;
	var card = user.card;
	var status = user.isRank;
	$("#status").val(status);
	
	if(!realName){realName="未设置";}
	else{realName=realName.substring(0,1)+"**";}
	if(safe=='true'){safe="已设置";}
	else{safe="未设置";}
	if(!card){card="未设置";}
	else{card=card.substring(0,4)+"***********"+card.substring(card.length-4,card.length);}	
	var nickName = user.nickName;
	if(!nickName){nickName="未设置";}
//	var isRank = user.isRank
//	if(isRank=="1"){isRank="参加";}
//	else{isRank="不参加";}
	var phone = user.phone;
	if(!phone){phone="未设置";}
	else{phone = phone.substring(0,3)+"****"+phone.substring(phone.length-4,phone.length);}
	var email = user.erroremial;
	if(!email){email="未设置"}
	var htmls = [
			'<li  class="mui-table-view-cell">真实姓名：'+realName+'</a></li>',
			'<li  class="mui-table-view-cell">身份证号：'+card+'</a></li>',
			'<li  class="mui-table-view-cell">',
			'	<a id="loginPwd" href="javascript:void(0)" class="mui-navigate-right">登录密码：******</a>',
			'</li>',
			'<li  class="mui-table-view-cell">',
			'	<a id="tranPwd" href="javascript:void(0)" class="mui-navigate-right">交易密码：******</a>',
			'</li>',
			'<li  class="mui-table-view-cell">',
			'	<a id="email" href="javascript:void(0)" class="mui-navigate-right">电子邮箱：'+email+'</a>',			
			'</li>',
			'<li  class="mui-table-view-cell">',
			'	<a id="safe" href="javascript:void(0)" class="mui-navigate-right">安全问题：'+safe+'</a>',
			'</li>',
			'<li  class="mui-table-view-cell">',
			'	<a id="phone" href="javascript:void(0)" class="mui-navigate-right">手机号码：'+phone+'</a>',
			'</li>',
			'<li class="mui-table-view-cell">',
			'	<a id="nickName" href="javascript:void(0)" class="mui-navigate-right">昵称：'+nickName+'</a>',
			'</li>',
			'<li class="mui-table-view-cell">',
			'	<span>是否参加投资排行榜：</span>',
			'   <div id="mySwitch" class="mui-switch mui-active">',
			'		<div class="mui-switch-handle"></div>',
			'	</div>',
			'</li>',
			'<li class="mui-table-view-cell">',
			'	<a id="promote" href="javascript:void(0)" class="mui-navigate-right">推广链接：</a>',
			'</li>'
	];
	
	return htmls.join(" ");
}

function addClick(){

	/**-------- 登录密码  --------*/
	var loginPwd = CmwMcore.$('loginPwd');
	MeventMgr.onTouchstart(loginPwd,function(event,self){		
		var cfg = {};
		MuiEventMgr.open('reset_login_pwd.html',cfg);
	});
	
	/**-------- 交易密码  --------*/
	var tranPwd = CmwMcore.$('tranPwd');
	MeventMgr.onTouchstart(tranPwd,function(event,self){
		var cfg = {};
		MuiEventMgr.open('reset_detail_pwd.html',cfg);	
	});
	
	/**-------- 修改邮箱  --------*/
	var email = CmwMcore.$('email');
	MeventMgr.onTouchstart(email,function(event,self){
		var email1 = $("#email1").val();
		if(email1=='未设置')
		MuiEventMgr.open('semail.html',{});	
		else
		MuiEventMgr.open('reset_email.html',{});	
	});
	
	/**-------- 修改安全问题 --------*/
	var safe = CmwMcore.$('safe');
	MeventMgr.onTouchstart(safe,function(event,self){
		var safe1 = $("#safe1").val();
		if(safe1=='未设置')
		MuiEventMgr.open('sproblem.html',{});	
		else
		MuiEventMgr.open('reset_safety_problem.html',{});	
	});
	
	/**-------- 修改手机号码--------*/
	var phone = CmwMcore.$('phone');
	MeventMgr.onTouchstart(phone,function(event,self){
		var cfg = {};
		MuiEventMgr.open('reset_phone.html',cfg);	
	});
	
	/**-------- 修改手机号码--------*/
	var nickName = CmwMcore.$('nickName');
	MeventMgr.onTouchstart(nickName,function(event,self){
		var cfg = {};
		MuiEventMgr.open('reset_name.html',cfg);	
	});
	
	/**-------- 我的银行卡--------*/
	var myBanklink = CmwMcore.$('myBanklink');
	MeventMgr.onTouchstart(myBanklink,function(event,self){
		var cfg = {};
		MuiEventMgr.open('../mybank/mybank.html',cfg);	
	});
	
	
}
